package com.xuecheng.manage_course.service;

import com.xuecheng.framework.domain.course.Interaction;
import com.xuecheng.framework.domain.course.request.InteractionRequest;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.manage_course.dao.InteractionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author Lyq
 * @version 1.0
 * @create 2020/5/20 19:09
 */
@Service
public class InteractionService {

    @Autowired
    InteractionRepository interactionRepository;

    //查询笔记
    public Interaction getNote(String courseId) {
        Optional<Interaction> optional = interactionRepository.findById(courseId);
        if (!optional.isPresent()){
            return null;
        }
        return optional.get();
    }

    //添加笔记
    public ResponseResult addNote(InteractionRequest interactionRequest) {
        Interaction interaction = new Interaction();
        interaction.setCourseId(interactionRequest.getCourseId());
        interaction.setUserId(interactionRequest.getUserId());
        interaction.setComment(interactionRequest.getComment());
        interaction.setCreateTime(interactionRequest.getCreateData());
        interactionRepository.save(interaction);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //删除笔记
    public ResponseResult delNote(String id) {
        interactionRepository.deleteById(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //查询问答
    public Interaction getQuestion(String courseId) {
        Optional<Interaction> optional = interactionRepository.findById(courseId);
        if (!optional.isPresent()){
            return null;
        }
        return optional.get();
    }

    //添加问答
    public ResponseResult addQuestion(InteractionRequest interactionRequest) {
        Interaction interaction = new Interaction();
        String courseId = interactionRequest.getCourseId();
        String userId = interactionRequest.getUserId();
        String comment = interactionRequest.getComment();
        String createData = interactionRequest.getCreateData();
        interaction.setCourseId(courseId);
        interaction.setUserId(userId);
        interaction.setComment(comment);
        interaction.setCreateTime(createData);
        interactionRepository.save(interaction);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //删除问答
    public ResponseResult delQuestion(String id) {
        interactionRepository.deleteById(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //添加回答
    public ResponseResult addAnswer(InteractionRequest interactionRequest) {
        Interaction interaction = new Interaction();
        String updateTime = interaction.getUpdateTime();
        interaction.setCourseId(interactionRequest.getCourseId());
        interaction.setUserId(interactionRequest.getUserId());
        interaction.setComment(interactionRequest.getComment());
        interaction.setUpdateTime(updateTime);
        interactionRepository.save(interaction);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //查询评论
    public Interaction getTalk(String courseId) {
        Optional<Interaction> optional = interactionRepository.findById(courseId);
        if (!optional.isPresent()){
            return null;
        }
        return optional.get();
    }

    //添加评论
    public ResponseResult addTalk(InteractionRequest interactionRequest) {
        Interaction interaction = new Interaction();
        interaction.setCourseId(interactionRequest.getCourseId());
        interaction.setUserId(interactionRequest.getUserId());
        interaction.setComment(interactionRequest.getComment());
        interaction.setUpdateTime(interaction.getUpdateTime());
        interactionRepository.save(interaction);
        return new ResponseResult(CommonCode.SUCCESS);
    }

    //删除评论
    public ResponseResult delTalk(String id) {
        interactionRepository.deleteById(id);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
